package com.tao.util;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

public class jdbcUtil {
	public static String DRIVERNAME = null;
	public static String URL = null;
	public static String USER = null;
	public static String PASSWORD = null;

	public static Connection conn = null;

	static {
		try {

			Properties props = new Properties();
			InputStream in = jdbcUtil.class.getClassLoader().getResourceAsStream("db.properties");
			props.load(in);

			DRIVERNAME = props.getProperty("drivername");
			URL = props.getProperty("url");
			USER = props.getProperty("user");
			PASSWORD = props.getProperty("password");
			Class.forName(DRIVERNAME);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static Connection getConnection() throws Exception {
		if (conn != null) {
			return conn;
		}
		conn = DriverManager.getConnection(URL, USER, PASSWORD);
		return conn;
	}

	public static void close(Connection conn, ResultSet rs, Statement st) throws Exception {
		if (st != null)
			st.close();
		if (rs != null)
			rs.close();
		if (conn != null)
			conn.close();
	}
}
